package com.pospi.pospihold.ui.cash_ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;

import com.pospi.pospihold.R;
import com.pospi.pospihold.dto.GoodsDto;
import com.pospi.pospihold.http.MaxNO;
import com.pospi.pospihold.ui.pay_ui.PayActivity;
import com.pospi.pospihold.ui.base.BaseActivity;
import com.pospi.pospihold.ui.pay_ui.PayNewActivity;
import com.pospi.pospihold.util.SaveOrderInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FastCheckActivity extends BaseActivity {

    @Bind(R.id.led)
    TextView led;
    @Bind(R.id.qingchu)
    Button qingchu;
    @Bind(R.id.cheng)
    Button cheng;
    @Bind(R.id.jia)
    Button jia;
    @Bind(R.id.deng)
    Button deng;
    @Bind(R.id.fast_vip)
    Button fastVip;
    @Bind(R.id.fast_pay)
    Button fastPay;

    private String num1 = "";
    private String num2 = "";
    private double num3 = 0;
    private String sign;
    private int mark = 0;
    private boolean flag = true;
    private boolean dotmark = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_fast_check);
        ButterKnife.bind(this);
        setTitle("快速收款");

    }

    @Override
    protected void onResume() {
        super.onResume();
        clear();
    }

    public void calculator(View v) {
        Button btn = (Button) v;
        if (mark == 0) {//指的是没有点击运算符的时候
            if (v.getId() == R.id.dian && num1.trim().isEmpty()) {
                num1 = "0.";
            } else if (num1.equals("0") && v.getId() != R.id.dian) {
                num1 = btn.getText().toString();
            } else {
                num1 = num1 + btn.getText().toString();
            }

            led.setText(num1);
        } else if (mark == 1) {//当点击了加法运算符
            if (v.getId() == R.id.dian && num2.trim().isEmpty()) {
                num2 = "0.";
            } else if (num2.equals("0") && v.getId() != R.id.dian) {
                num2 = btn.getText().toString();
            } else {
                num2 = num2 + btn.getText().toString();
            }
            led.setText(num1 + sign + num2);
        } else {
            num1 = String.valueOf(num3);
            num2 = num2 + btn.getText().toString();
            led.setText(num2);
        }

    }

    public void clickYunSuan(Button btn) {

        if (num2.isEmpty()) {//如果num2为空的时候，
            if (flag) {
                sign = btn.getText().toString();
                mark++;
                flag = false;
                dotmark = true;
            }
        } else {
            equl();
            num1 = led.getText().toString();
            num2 = "";
        }
    }

    //等于和付款的时候的运算
    public void equl() {
        if (!num2.isEmpty()) {
            flag = true;
            dotmark = true;
            if (sign.equals("+")) {
                double x = Double.parseDouble(num1);
                double y = Double.parseDouble(num2);
                num3 = x + y;
                led.setText(String.valueOf(num3));
                num2 = "";
            } else if (sign.equals("X")) {
                double x = Double.parseDouble(num1);
                double y = Double.parseDouble(num2);
                num3 = x * y;
                led.setText(String.valueOf(num3));
                num2 = "";
            }
        }

    }


    @OnClick({R.id.qingchu, R.id.cheng, R.id.jia, R.id.deng, R.id.fast_vip, R.id.fast_pay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.qingchu:
                clear();
                break;
            case R.id.cheng:
                if (!num1.trim().isEmpty()) {
                    clickYunSuan(cheng);
                }
                break;
            case R.id.jia:
                if (!num1.trim().isEmpty()) {
                    clickYunSuan(jia);
                }
                break;
            case R.id.deng:
                equl();
                break;
            case R.id.fast_vip:
                break;
            case R.id.fast_pay:
                equl();
                double ysMoney = Double.parseDouble(led.getText().toString());
                if (ysMoney <= 0) {
                    showToast("金额应大于0");
                } else {
                    GoodsDto dto = new GoodsDto();
                    dto.setNum(1);
                    dto.setPrice(ysMoney);
                    dto.setName("自助收银");
                    dto.setCode(null);
                    List<GoodsDto> dtos = new ArrayList<>();
                    dtos.add(dto);
                    //把商品信息进行存储
                    SaveOrderInfo.saveAsJson(getApplicationContext(), dtos);

                    Intent intent = new Intent(FastCheckActivity.this, PayNewActivity.class);
                    intent.putExtra("data", ysMoney+"");
                    intent.putExtra("remark", "FastCheckActivity");
                    startActivity(intent);

                    Animation animation = new TranslateAnimation(0, -1, 0, 0);
                    animation.setDuration(5000);//设置动画持续时间
                    animation.start();
                }
                break;
        }
    }

    public void clear() {
        num1 = "";
        num2 = "";
        num3 = 0;
        mark = 0;
        sign = "";
        led.setText("0");
        flag = true;
        dotmark = true;

    }
}
